import { dbUtils } from '../core'

export interface StoreShopDeskOrderRow {
    id: string
    order_no: string
    store_id: string
    shop_id: string
    user_id: string
    open_clerk_id: string
    end_clerk_id: string
    record_ids: string
    desk_name: string
    foodgroup_id: string
    foodgroup_money: number
    menu_id: string
    model_money: number
    order_money: number
    un_discount_money: number
    reduce_money: number
    coupon_id: string
    coupon_money: number
    service_money: number
    is_vip: number
    order_discount: number
    user_discount: number
    pay_money: number
    real_pay_money: number
    unclosing_real_pay_money: number
    is_book: number
    book_id: string
    is_present: number
    calculate_type: number
    pay_status: number
    pay_time: number
    is_unclosing: number
    remarks: string
    is_delete: number
    create_time: number
    update_time: number
}

export const storeShopDeskOrderRepository = {
    upsertBatch(rows: StoreShopDeskOrderRow[]): void {
        if (!rows || rows.length === 0) return
        dbUtils.transaction(() => {
            const sql = `INSERT INTO store_shop_desk_order (
                id, order_no, store_id, shop_id, user_id, open_clerk_id, end_clerk_id, record_ids,
                desk_name, foodgroup_id, foodgroup_money, menu_id, model_money, order_money,
                un_discount_money, reduce_money, coupon_id, coupon_money, service_money, is_vip,
                order_discount, user_discount, pay_money, real_pay_money, unclosing_real_pay_money,
                is_book, book_id, is_present, calculate_type, pay_status, pay_time, is_unclosing,
                remarks, is_delete, create_time, update_time
            ) VALUES (
                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
            )
            ON CONFLICT(id) DO UPDATE SET
                order_no=excluded.order_no,
                store_id=excluded.store_id,
                shop_id=excluded.shop_id,
                user_id=excluded.user_id,
                open_clerk_id=excluded.open_clerk_id,
                end_clerk_id=excluded.end_clerk_id,
                record_ids=excluded.record_ids,
                desk_name=excluded.desk_name,
                foodgroup_id=excluded.foodgroup_id,
                foodgroup_money=excluded.foodgroup_money,
                menu_id=excluded.menu_id,
                model_money=excluded.model_money,
                order_money=excluded.order_money,
                un_discount_money=excluded.un_discount_money,
                reduce_money=excluded.reduce_money,
                coupon_id=excluded.coupon_id,
                coupon_money=excluded.coupon_money,
                service_money=excluded.service_money,
                is_vip=excluded.is_vip,
                order_discount=excluded.order_discount,
                user_discount=excluded.user_discount,
                pay_money=excluded.pay_money,
                real_pay_money=excluded.real_pay_money,
                unclosing_real_pay_money=excluded.unclosing_real_pay_money,
                is_book=excluded.is_book,
                book_id=excluded.book_id,
                is_present=excluded.is_present,
                calculate_type=excluded.calculate_type,
                pay_status=excluded.pay_status,
                pay_time=excluded.pay_time,
                is_unclosing=excluded.is_unclosing,
                remarks=excluded.remarks,
                is_delete=excluded.is_delete,
                create_time=excluded.create_time,
                update_time=excluded.update_time
            `

            for (const r of rows) {
                dbUtils.execute(sql, [
                    r.id, r.order_no, r.store_id, r.shop_id, r.user_id, r.open_clerk_id, r.end_clerk_id, r.record_ids,
                    r.desk_name, r.foodgroup_id, r.foodgroup_money, r.menu_id, r.model_money, r.order_money,
                    r.un_discount_money, r.reduce_money, r.coupon_id, r.coupon_money, r.service_money, r.is_vip,
                    r.order_discount, r.user_discount, r.pay_money, r.real_pay_money, r.unclosing_real_pay_money,
                    r.is_book, r.book_id, r.is_present, r.calculate_type, r.pay_status, r.pay_time, r.is_unclosing,
                    r.remarks, r.is_delete, r.create_time, r.update_time
                ])
            }
        })
    },

    listByStore(store_id: string, shop_id?: string) {
        if (shop_id) {
            return dbUtils.query(
                `SELECT * FROM store_shop_desk_order WHERE store_id = ? AND shop_id = ? ORDER BY create_time DESC`,
                [store_id, shop_id]
            )
        }
        return dbUtils.query(
            `SELECT * FROM store_shop_desk_order WHERE store_id = ? ORDER BY create_time DESC`,
            [store_id]
        )
    }
}

export default storeShopDeskOrderRepository


